import fetch from 'isomorphic-unfetch'
// import dynamic from 'next/dynamic'
// const DynamicPublish = dynamic(
//   import('./publish'),
//   {
//     ssr: false   //这个要加上,禁止使用 SSR
//   }
// )
import { globalConfig } from './lib';

const getTargetUrl = () => {
    if (!window.location){
        return null;
    } 
    let url = location.pathname;
    let search = location.search
    if (search) {
        url += search;
    }
    return url;
};
const getUrlParam = (key) => {
    if (!window.location){
        return null;
    } 
    let value = '';
    let isFound = false;
    const location = window.location;
    if (location.search.indexOf('?') === 0 && location.search.indexOf('=') > 1) {
        let arrSource = location.search.substring(1, location.search.length).split('&');
        let i = 0;
        while (i < arrSource.length && !isFound) {
            if (arrSource[i].indexOf('=') > 0) {
                const tmpArr = arrSource[i].split('=');
                if (tmpArr[0].toLowerCase() === key.toLowerCase()) {
                    value = decodeURIComponent(tmpArr[1]);
                    isFound = true;
                }
            }
            i++;
        }
    }
    return value;
};
const patterns = {
    email: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/,
    tel: [/^0{0,1}(13[0-9]|15[0-9]|17[0-9]|18[0-9])[0-9]{8}$/, /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$/],
    account: /^[A-Za-z0-9_]{4,20}$/,
    password: /^[A-Za-z0-9_]{4,20}$/,
};
const testPattern = (key, value) => {
    var pattern = patterns[key];
    if (Array.isArray(pattern)) {
        let flag = false
        for (var i = 0; i < pattern.length; i++) {
            if (pattern[i].test(value)) {
                flag = true;
            }
        }
        return flag;
    }
    if (pattern.test(value)) {
        return true;
    }
    return false;
};
const jsonToForm = json => {
    let form = '';
    Object.keys(json).forEach((key, index) => {
        if (index) {
            form += '&'
        }
        form += (key + '=' + json[key]);
    });
    return form;
};
const fetchData = (params) => {
    const data = {
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'include', // *same-origin, include, omit
        headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8' // tp 不识别 application/json 使用 app/json
        },
        method: params.method || 'GET', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // *same-origin, no-cors, cors,
        redirect: 'follow', // *follow, manual, error
        referrer: 'client', // *client, no-referrer
    }
    if (params.data) {
        if (params.method && (params.method === 'POST' || params.method === 'post')) {
            data.body = jsonToForm(params.data); // must match 'Content-Type' header
        } else {
            params.url += ('?' + jsonToForm(params.data));
        }
    }
    // Default options are marked with *
    return fetch(params.url, data)
        .then(response => {
            if (response.ok) {
                return response.json();
            }
            return {
                error: 1,
                msg: '网络错误'
            };
        })
};
export { fetchData, testPattern, getUrlParam , getTargetUrl };